package controllers;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import dao.DaoClient;
import dao.HibernateUtil;
import dao.interfaces.IDaoClient;

import beans.Client;
import beans.Passager;

@Controller
public class ControllerCompte {
	public ModelAndView inscription(HttpSession session) {
		ModelAndView mv = new ModelAndView();
		Client c = (Client) session.getAttribute("user");

		mv.addObject("Client", c);

		mv.setViewName("modificationCompte");
		// mv.setViewName
		return mv;
	}

	@RequestMapping(value = "/compte")
	public ModelAndView afficheCompte(HttpSession session) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("modificationCompte");
		Client c = (Client) session.getAttribute("user");

		Passager p = new Passager();
		mv.addObject("Passager", p);

		mv.addObject("Client", c);
		return mv;
	}

	@RequestMapping(value = "/supressCompte")
	public String supprimerCompte(HttpSession session) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("modificationCompte");
		Client c = (Client) session.getAttribute("user");

		IDaoClient daoClient = new DaoClient();

		HibernateUtil.getSessionFactory().getCurrentSession()
				.beginTransaction();

		daoClient.delete(c);

		HibernateUtil.getSessionFactory().getCurrentSession().getTransaction()
				.commit();
		session.removeAttribute("user");
		return "redirect:/home";
	}

	@RequestMapping(value = "/updateCompte", method = RequestMethod.POST)
	public ModelAndView add(@ModelAttribute("Client") @Valid Client recu,
			BindingResult results,
			HttpSession session) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("modificationCompte");

		// SI le formulaire est mal rempli, on le renvoie

		if (results.hasErrors()) {
			mv.addObject("Client", recu);

			System.out.println("On Fait PAS le merge");
			System.out.println(results.toString());
		} else {
			
			mv.addObject("Client", recu);

			System.out.println("On Fait le merge");
			IDaoClient daoClient = new DaoClient();

			HibernateUtil.getSessionFactory().getCurrentSession()
					.beginTransaction();

			daoClient.merge(recu);
			
			Client c = daoClient.findById(recu.getCliNum());
			
			HibernateUtil.getSessionFactory().getCurrentSession()
					.getTransaction().commit();
			
			session.removeAttribute("user");
			session.setAttribute("user", c);

		}
		Passager p = new Passager();

		mv.addObject("Passager", p);
		return mv;
	}

	// @RequestMapping(value = "/updateCompte",method = RequestMethod.POST)
	// public ModelAndView delete(@ModelParameter("id")){
	//
	// }

}
